﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace practice
{
    
    public class  MathTool
    {
        /// <summary>
        ///冒泡排序 
        /// </summary>
        /// <param name="list"></param>
        public int[] Sort(params int[] list)
        {
            
            for (int j = 0; j < list.Length; j++)
            { 
                //交换事件
                bool exchanged = false;
                //从第一对数开始判断一轮
                for (int i = 0; i < list.Length-1; i++)
                {
                    //判断是否交换
                    if (i+1<= list.Length && list[i] < list[i + 1])
                    {
                       int temp = list[i+1];
                        list[i + 1] = list[i];
                        list[i] = temp;
                        exchanged = true;
                    }
                }
                //一轮中如果没有交换，则已经排好，此时退出循环
                if (exchanged == false)
                {
                    break;
                } 
            }
            //返回
            return list;
        }
        /// <summary>
        /// ref应用，
        /// </summary>
        /// <param name="x">形式参数</param>
        /// <param name="y">形式参数</param>
        /// <param name="z">形式参数</param>
        /// <param name="max">形式参数</param>
        /// <param name="mid">形式参数</param>
        /// <param name="min">形式参数</param>
        public void Swap(ref int x ,ref  int y,ref int z,out int max, out int mid,out int min)
        {
            bool exchanged = false;
            for (int i = 0; i < 3; i++)
            {
                
                if (x < y)
                {
                    int temp = x;
                     x = y;
                    y = temp;
                    exchanged = true;
                }
                if(y < z)
                {
                    int temp = z;
                    z = y;
                    y = temp;
                    exchanged = true;
                }
                if(exchanged == false)
                {
                    break;
                }
            }
                    max = x;
                    mid = y;    
                    min = z;
        }

    }
    internal class Program
    {
        
        static void Main(string[] args)
        {   int a = 0,b = 0, c = 0;
            
            int[] arr = new int[3] { 3, 6, 72 };
            MathTool mathTool = new MathTool();
            mathTool.Swap(ref arr[0], ref arr[1], ref arr[2], out a, out b, out c);
             Console.WriteLine($"使用ref排序，为：{a}， {b}， {c}，" +
                 $"而使用时，为{arr[0] + arr[1] + arr[2]}");
            int[] list = mathTool.Sort(2, 6, 4, 8, 79, 7, 42);
            for (int j = 0; j < list.Length; j++)
            {
                Console.Write(list[j] + "\t");
            }

        }
    }
}
